package com.example.demo.servlet;

import com.example.demo.dao.Admindao;
import com.example.demo.dao.UserDao;
import com.example.demo.entity.AdminVo;
import com.example.demo.entity.UserVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private UserDao userDao = new UserDao();
    private Admindao adminDao = new Admindao();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        UserVo userVo = null;
        AdminVo adminVo=null;
        try {
            userVo = userDao.findByName(username);
            adminVo = adminDao.findByName(username);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (userVo!=null&&userVo.getPassword().equals(password)) {
            req.getSession(true).setAttribute("user", userVo);
                req.getRequestDispatcher("/buyer.jsp").forward(req,resp);
        }
        if (adminVo!=null&&adminVo.getPassword().equals(password)){
            req.getSession(true).setAttribute("user", adminVo);
            req.getRequestDispatcher("/bookmanage.jsp").forward(req,resp);

        }
    }
}
